const app = getApp();

// 登录方法
const login = () => {
  return new Promise((resolve, reject) => {
    wx.login({
      success: (res) => {
        if (res.code) {
          // 这里应该发送code到后端获取用户信息
          // 模拟登录成功
          wx.setStorageSync('isLoggedIn', true);
          resolve(true);
        } else {
          reject(new Error('登录失败'));
        }
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
};

// 登出方法
const logout = () => {
  wx.setStorageSync('isLoggedIn', false);
  return Promise.resolve(true);
};

// 检查登录状态
const checkLogin = () => {
  return wx.getStorageSync('isLoggedIn') || false;
};

// 获取用户信息
const getUserInfo = () => {
  return new Promise((resolve, reject) => {
    wx.getUserProfile({
      desc: '用于完善会员资料',
      success: (res) => {
        resolve(res.userInfo);
      },
      fail: (err) => {
        reject(err);
      }
    });
  });
};

module.exports = {
  login,
  logout,
  checkLogin,
  getUserInfo
};